package com.acdc.utils;

import java.security.PrivateKey;
import java.security.Signature;
import java.util.Base64;
import java.nio.charset.StandardCharsets;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import java.io.InputStream;

/**
 * 支付签名验证测试工具
 * 用于调试和验证签名生成是否正确
 */
public class PaymentSignatureTest {
    
    /**
     * 测试JSAPI支付签名生成
     */
    public static void testJSAPISignature() {
        try {
            String appId = "wxbff98f0816c4c35c";
            String timeStamp = String.valueOf(System.currentTimeMillis() / 1000);
            String nonceStr = "test_nonce_str_" + System.currentTimeMillis();
            String packageStr = "prepay_id=test_prepay_id";
            
            System.out.println("=== JSAPI支付签名测试 ===");
            System.out.println("appId: " + appId);
            System.out.println("timeStamp: " + timeStamp);
            System.out.println("nonceStr: " + nonceStr);
            System.out.println("package: " + packageStr);
            
            // 构建签名串
            String signatureStr = String.format("%s\n%s\n%s\n%s\n",
                    appId, timeStamp, nonceStr, packageStr);
            System.out.println("签名串: " + signatureStr);
            
            // 生成签名
            String signature = SignUtils.sign("1709577471", appId, timeStamp, nonceStr, packageStr);
            System.out.println("生成的签名: " + signature);
            
            // 验证签名串格式
            System.out.println("签名串长度: " + signatureStr.length());
            System.out.println("签名串字节数: " + signatureStr.getBytes(StandardCharsets.UTF_8).length);
            
        } catch (Exception e) {
            System.err.println("签名测试失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    /**
     * 测试私钥加载
     */
    public static void testPrivateKeyLoading() {
        try {
            System.out.println("=== 私钥加载测试 ===");
            
            PrivateKey privateKey = loadPrivateKey("classpath:cert/apiclient_key.pem");
            System.out.println("私钥算法: " + privateKey.getAlgorithm());
            System.out.println("私钥格式: " + privateKey.getFormat());
            System.out.println("私钥长度: " + privateKey.getEncoded().length);
            
        } catch (Exception e) {
            System.err.println("私钥加载测试失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    /**
     * 加载私钥
     */
    private static PrivateKey loadPrivateKey(String filename) throws Exception {
        if (filename.startsWith("classpath:")) {
            String classPath = filename.substring("classpath:".length());
            Resource resource = new ClassPathResource(classPath);
            try (InputStream inputStream = resource.getInputStream()) {
                return PemUtil.loadPrivateKey(inputStream);
            }
        } else {
            throw new RuntimeException("不支持的文件路径格式: " + filename);
        }
    }
    
    /**
     * 主方法，用于测试
     */
    public static void main(String[] args) {
        System.out.println("开始支付签名测试...");
        
        // 测试私钥加载
        testPrivateKeyLoading();
        
        // 测试签名生成
        testJSAPISignature();
        
        System.out.println("测试完成！");
    }
}
